package com.fire.security.controller;
import com.fire.security.core.page.TableDataInfo;
import com.fire.security.domain.Device;
import com.fire.security.domain.Result;
import com.fire.security.dto.DeviceStatusCountDto;
import com.fire.security.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <h1>设备管理</h1>
 * */
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {

    @Autowired
    private DeviceService deviceService;


    /**
     * <h2>
     *     查询所有device列表业务方法的url接口/device-list
     * </h2>
     * */
    @GetMapping("/device-list")
    public TableDataInfo deviceList(Device device)
    {
        startPage();
        List<Device> list = deviceService.queryAllDevice(device);
        return getDataTable(list);
    }

    /**
     * <h2>
     *     通过用户id查询设备列表
     * </h2>
     * */
    @GetMapping("/device-list-by-user-id")
    public TableDataInfo deviceListByUserId(@RequestParam("userId") String userId)
    {
        startPage();
        List<Device> list = deviceService.queryDeviceByUserId(userId);
        return getDataTable(list);
    }

    /**
     * <h2>
     *     根据状态查询设备数量的url接口/device-status-count
     * </h2>
     * */
    @GetMapping("/device-status-count")
    public List<DeviceStatusCountDto> deviceStatusCount()
    {
        return deviceService.countDeviceByStatus();
    }

    /**
     * <h2>
     *     删除device业务方法的url接口/device-delete/{id}
     * </h2>
     * */
    @DeleteMapping("/device-delete")
    public Result deleteDevice(@RequestParam("id") String id)
    {
        int rows = deviceService.deleteById(id);
        return rows > 0 ? Result.ok() : Result.err(501, "删除失败");
    }

    /**
    * <h2>
     *     更新device业务方法的url接口/device-update
    * </h2>
    * */
    @PutMapping("/device-update")
    public Result updateDevice(@RequestBody Device device)
    {
        int rows = deviceService.updateById(device);
        return rows > 0 ? Result.ok() : Result.err(501, "更新失败");
    }

    /**
     * <h2>
     *     新增device业务方法的url接口/device-add
     * </h2>
     * */
    @PostMapping("/device-add")
    public Result addDevice(Device device)
    {
        int rows = deviceService.insert(device);
        return rows > 0 ? Result.ok() : Result.err(501, "新增失败");
    }

    /**
     * <h2>
     *     更新device状态业务方法的url接口/device-status-update
     * </h2>
     * */
    @PutMapping("/device-status-update")
    public Result updateDeviceStatus(Device device)
    {
        int rows = deviceService.updateStatusById(device);
        return rows > 0 ? Result.ok() : Result.err(501, "更新失败");
    }

    /**
     * <h2>
     *     更新device服务时间业务方法的url接口/device-service-time-update
     * </h2>
     * */
    @PutMapping("/device-service-time-update")
    public Result updateDeviceServiceTime(Device device)
    {
        int rows = deviceService.updateServiceTimeById(device);
        return rows > 0 ? Result.ok() : Result.err(501, "更新失败");
    }


    /**
     * <h2>
     *     上传图片的业务方法接口/device/img-upload
     * </h2>
     * */
    //上传路径
    private String uploadPath = "classpath:static/img/upload";
    @CrossOrigin
    @PostMapping("/img-upload")
    public Result uploadImg(MultipartFile file){

        try {
            //拿到图片上传到的目录(类路径classes下的static/img/upload)的File对象
            File uploadDirFile = ResourceUtils.getFile(uploadPath);
            //拿到图片上传到的目录的磁盘路径
            String uploadDirPath = uploadDirFile.getAbsolutePath();
            //拿到图片保存到的磁盘路径
            String fileUploadPath = uploadDirPath + "\\" + file.getOriginalFilename();
            //保存图片
            file.transferTo(new File(fileUploadPath));
            //成功响应
            return Result.ok("图片上传成功！");
        } catch (IOException e) {
            //失败响应
            return Result.err(Result.CODE_ERR_BUSINESS, "图片上传失败！");
        }
    }

}




